package com.cooge.jiexi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;


public class IpStatistics {
	
	public static void main(String[] args) throws Exception {
		File file = new File("ip___"+System.currentTimeMillis()+".txt");
		FileOutputStream  fos = new FileOutputStream(file);
		OutputStreamWriter opw= new OutputStreamWriter(fos,"utf-8");
		PrintWriter pw = new PrintWriter(opw, true);
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		String paths[] = {"E:\\2015-07-07-0000-2330_client.kanketv.com.cn.log"};
		int  i_ = 0;
		for(String path:paths){
			FileInputStream fis = new FileInputStream(new File(path));
			InputStreamReader isr  = new InputStreamReader(fis);
			@SuppressWarnings("resource")
			BufferedReader br = new BufferedReader(isr);
			
			String line = null;
			
			while((line=br.readLine())!=null){
				String[] ips = Test.getIPs(line);
				for(String ip:ips){
                    i_++;
					System.out.println(ip+"__num__"+i_);
					Integer num =  map.get(ip);
					if(num==null){
						map.put(ip, 1);
					}else{
						map.put(ip, num+1);
					}
				}
			}
			
		}
		
		
		
		
		map = Test.sortByValue(map);
		
		Iterator<Entry<String, Integer>> set = map.entrySet().iterator();
		
		
		while(set.hasNext()){
			Entry<String, Integer> ent=  set.next();
			
//			if(ent.getValue()>1000){
				pw.println("IP:"+ent.getKey()+"_____次数:"+ent.getValue());
				pw.flush();
//			}
			
		}
		pw.close();
	}

}
